package com.object.utils;

import com.alibaba.fastjson.JSON;
import com.alibaba.fastjson.JSONObject;
import com.aliyun.oss.ClientException;
import com.aliyuncs.DefaultAcsClient;
import com.aliyuncs.IAcsClient;
import com.aliyuncs.profile.DefaultProfile;
import com.aliyuncs.vod.model.v20170321.CreateUploadVideoRequest;
import com.aliyuncs.vod.model.v20170321.CreateUploadVideoResponse;
import com.aliyuncs.vod.model.v20170321.GetPlayInfoRequest;
import com.aliyuncs.vod.model.v20170321.GetPlayInfoResponse;
import com.google.gson.Gson;
import io.swagger.annotations.ApiOperation;
import lombok.SneakyThrows;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import java.util.HashMap;

@RestController
@RequestMapping("/alibaba/vod")
public class AlibabaCloudVodUtil
{
    /**
     * 阿里巴巴视频点播的key
     */
    private String accessKey = "";
    /**
     * 阿里巴巴视频点播的key的value
     */
    private String secretKey = "";
    /**
     * 阿里巴巴视频点播的上传视频的地区
     */
    private static String ApiRegion = "cn-shanghai";

    @SneakyThrows
    @ApiOperation("视频上传凭证")
    @GetMapping("/videoUploadCredentials")
    public Object uploadVideoAlyum(String filename, String title)
    {
        DefaultProfile profile = DefaultProfile.getProfile(ApiRegion, accessKey, secretKey);
        IAcsClient client = new DefaultAcsClient(profile);
        CreateUploadVideoRequest request = new CreateUploadVideoRequest();
        request.setFileName(filename);
        request.setTitle(title);
        CreateUploadVideoResponse response = client.getAcsResponse(request);
        //   System.out.println(new Gson().toJson(response));
        return response;
    }


    @ApiOperation("根据视频的videoId 去获取播放地址")
    @GetMapping("/{videoId}")
    public Object main(@PathVariable("videoId") String videoId)
    {
        DefaultProfile profile = DefaultProfile.getProfile(ApiRegion, accessKey, secretKey);
        IAcsClient client = new DefaultAcsClient(profile);

        GetPlayInfoRequest request = new GetPlayInfoRequest();
        request.setVideoId(videoId);
        HashMap<String, Object> map = new HashMap<>();
        try
        {
            GetPlayInfoResponse response = client.getAcsResponse(request);
            String toJson = new Gson().toJson(response);
            JSONObject playInfoObj = JSON.parseObject(toJson).getJSONArray("playInfoList").getJSONObject(0);
            String playURL = (String) playInfoObj.get("playURL");
            Integer width = (Integer) playInfoObj.get("width");
            Integer height = (Integer) playInfoObj.get("height");
            map.put("playURL", playURL);
            map.put("width", width);
            map.put("height", height);
            //     System.out.println(new Gson().toJson(response));
        }
        catch (ClientException e)
        {
            map.put("error", e);
        }
        catch (com.aliyuncs.exceptions.ServerException e)
        {
            return Q.error().message("视频转码中...");
        }
        catch (com.aliyuncs.exceptions.ClientException e)
        {
            return Q.error().message("视频转码中...");
        }
        return Q.ok().put(map);
    }
}
